﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Globalization;
using System.Threading;

namespace Talkng_head
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //this.Shown += LoadHead;
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(openAndSpeakFile));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        private void openAndSpeakFile()
        {
            using (OpenFileDialog openFileDialog1 = new OpenFileDialog()) //tworzy okienko do wyboru pliku
            {
                openFileDialog1.InitialDirectory = Application.StartupPath + "\\textFiles"; 
                openFileDialog1.Filter = "text files|*.txt";
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.RestoreDirectory = false;

                if (openFileDialog1.ShowDialog() == DialogResult.OK) //otwiera okienko do wyboru pliku
                {
                    try
                    {
                        using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) //odczytywanie plików
                        {
                            String line;
                            while ((line = sr.ReadLine()) != null) //odczytywanie tekstu linijka po linijce
                            {
                                var speaker = new SpeechSynthesizer(); //tworzenie syntezatora
                                speaker.Speak(line); //wypowiadanie tekstu
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine("The file could not be read:"); //obsługa błędu
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        private void speakFile(object fileName)
        {
            try
            {
                using (StreamReader sr = new StreamReader(fileName.ToString())) //odczytywanie plików
                {
                    String line;
                    while ((line = sr.ReadLine()) != null) //odczytywanie tekstu linijka po linijce
                    {
                        var speaker = new SpeechSynthesizer(); //tworzenie syntezatora
                        speaker.Speak(line); //wypowiadanie tekstu
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("The file could not be read:"); //obsługa błędu
                Console.WriteLine(exception.Message);
            }
        }

        private void femaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.headControl.LoadFemaleHead(); //ładowanie damskiej głowy
        }


        private void sayHaloToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ttsVoice = new SpeechSynthesizer();
            ttsVoice.Speak("An interactive talking head interface helps you to solve problems connected with computer skills and everyday life."); //przykładowe gadanie
        }
        private void firstStepsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ttsVoice = new SpeechSynthesizer();
            ttsVoice.Speak("First you have to select the load head button and choose one of head models from the list.");
        }
        private void maleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.headControl.LoadMaleHead();
        }


        private void fridgeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(speakFile));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(Application.StartupPath + "\\textFiles\\CloseTheFridge.txt");
        }

        private void faucetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(speakFile));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(Application.StartupPath + "\\textFiles\\TurnOffFaucet.txt");
        }

        private void stoveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(speakFile));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(Application.StartupPath + "\\textFiles\\CheckTheStove.txt");
        }


       

        
    }
}
